//
//  IGCircleprogress.m
//  jingdongfang
//
//  Created by BOE on 2017/11/16.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGCircleprogress.h"

@interface IGCircleprogress ()

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) CAShapeLayer *backLayer;
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (nonatomic, strong) UILabel *progressLabel;

@property (nonatomic, assign) CGFloat lastProgress;/**<上次进度 0-1 */

@end

@implementation IGCircleprogress

#define backRealWidth 180
#define backRealHeight 160
#define realWidth 60
#define lineWidthP 10

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.5];
        
        [self addSubview:self.backView];
        [self.backView.layer addSublayer:self.backLayer];
        [self.backView.layer addSublayer:self.progressLayer];

        [self.backView addSubview:self.progressLabel];
    }
    return self;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake((KSCREENWIDTH - backRealWidth)/2.0, (KSCREENHEIGHT - backRealHeight)/2.0, backRealWidth, backRealHeight)];
        _backView.layer.cornerRadius = 10;
        _backView.backgroundColor = [UIColor c3Color];
        
        UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, backRealHeight - 40, backRealWidth, 20)];
//        tipsLabel.text = @"正在上传,请稍候...";
        tipsLabel.text = NSLocalizedString(@"Uploading, please wait.....", nil);
        tipsLabel.textAlignment = NSTextAlignmentCenter;
        tipsLabel.font = [UIFont boldSystemFontOfSize:14.0];
        tipsLabel.textColor = [UIColor c4Color];
        [_backView addSubview:tipsLabel];
    }
    return _backView;
}

- (CAShapeLayer *)backLayer {
    if (!_backLayer) {
        
        _backLayer = [CAShapeLayer layer];
        
        _backLayer.frame = CGRectMake((backRealWidth - realWidth)/2.0, (backRealHeight - realWidth)/2.0 - 20, realWidth, realWidth);
        
        _backLayer.fillColor = [UIColor clearColor].CGColor;//填充色
        _backLayer.lineWidth = lineWidthP;
        _backLayer.strokeColor = [UIColor c5Color].CGColor;
        _backLayer.lineCap = @"round";
        
        UIBezierPath *backCirclePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(realWidth/2.0, realWidth/2.0) radius:realWidth/2.0 startAngle:0 endAngle:2*M_PI clockwise:YES];
        self.backLayer.path = backCirclePath.CGPath;
    }
    return _backLayer;
}

- (CAShapeLayer *)progressLayer {
    if (!_progressLayer) {
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.frame = CGRectMake((backRealWidth - realWidth)/2.0, (backRealHeight - realWidth)/2.0 - 20, realWidth, realWidth);
        
        _progressLayer.fillColor = [UIColor clearColor].CGColor;//填充色
        _progressLayer.lineWidth = lineWidthP;
        _progressLayer.strokeColor = [UIColor appColor].CGColor;
        _progressLayer.lineCap = @"round";
        
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(realWidth/2.0, realWidth/2.0) radius:realWidth/2.0 startAngle:-M_PI_2 endAngle:3*M_PI_2 clockwise:YES];
        _progressLayer.path = circlePath.CGPath;
        _progressLayer.strokeEnd = 0.0;
    }
    return _progressLayer;
}

- (UILabel *)progressLabel {
    if (!_progressLabel) {
        _progressLabel = [[UILabel alloc] init];
        _progressLabel.textColor = [UIColor blackColor];
        _progressLabel.textAlignment = NSTextAlignmentCenter;
        _progressLabel.font = [UIFont systemFontOfSize:12];
        _progressLabel.text = @"0%";
        _progressLabel.frame = CGRectMake(0, -20, backRealWidth, backRealHeight);
    }
    return _progressLabel;
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    if (_progress < 0) {
        _progress = 0;
    }
    if (_progress > 1) {
        _progress = 1;
    }
    if (_progress < 0.95 && (_progress - _lastProgress) < 0.01) {
        return;
    }
    
    [self startAnimation];
}

#define durationT 0.1
- (void)startAnimation {
    
    //线条动画
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnima.duration = durationT;
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnima.fromValue = [NSNumber numberWithFloat:_lastProgress];
    pathAnima.toValue = [NSNumber numberWithFloat:_progress];
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [self.progressLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
    
    self.progressLabel.text = [NSString stringWithFormat:@"%.0f%%", _progress*100];
    
    _lastProgress = _progress;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
